package com.mollytask.energyManager;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.mollytask.R;
import com.mollytask.bean.Institution;
import com.mollytask.bean.Message;
import com.mollytask.bean.MyUser;
import com.mollytask.bean.SuperAdmin;
import com.mollytask.util.DialogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

public class SendMessageActivity extends AppCompatActivity {


    private List<Institution> mInstitutions = new ArrayList<>();
    private List<SuperAdmin> mSuperAdmins = new ArrayList<>();

    private MyUser myUser;


    @BindView(R.id.back)
    ImageView mBack;
    @BindView(R.id.message)
    EditText mContent;
    @BindView(R.id.send)
    ImageView mSend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_message);

        ButterKnife.bind(this);

        myUser = BmobUser.getCurrentUser(MyUser.class);


        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MaterialDialog dialog = new MaterialDialog.Builder(SendMessageActivity.this)
                        .title("发布确认")
                        .content("确认要发布吗？")
                        .positiveText("发布")
                        .negativeText("取消")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                sendMessage(mContent.getText().toString());
                            }
                        })
                        .onNegative(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                dialog.dismiss();
                            }
                        })
                        .show();
            }
        });


    }

    public void sendMessage(String content) {

        BmobQuery<Institution> queryIn = new BmobQuery<>();
        queryIn.addWhereNotEqualTo("mInCode", myUser.getCode());
        queryIn.findObjects(new FindListener<Institution>() {
            @Override
            public void done(List<Institution> list, BmobException e) {
                if (e == null) {
                    mInstitutions = list;
                    findSuperAdmin(content);

                }
            }
        });


    }


    public void findSuperAdmin(String content) {
        BmobQuery<SuperAdmin> querySu = new BmobQuery<>();
        querySu.findObjects(new FindListener<SuperAdmin>() {
            @Override
            public void done(List<SuperAdmin> list, BmobException e) {
                if (e == null) {
                    mSuperAdmins = list;


                    sendToInstitution(content);
                }
            }
        });
    }


    public void sendToInstitution(String content) {

        for (int i = 0; i < mInstitutions.size(); i++) {
            Message message = new Message();
            message.setmContent(content);
            message.setType(1);
            message.setmFromCode(myUser.getCode());
            message.setmToCode(mInstitutions.get(i).getmInCode());
            message.save(new SaveListener<String>() {
                @Override
                public void done(String s, BmobException e) {

                    sendToSuperAdmin(content);
                }
            });
        }
    }

    public void sendToSuperAdmin(String content) {

        for (int i = 0; i < mSuperAdmins.size(); i++) {
            Message message = new Message();
            message.setmContent(content);
            message.setType(1);
            message.setmFromCode(myUser.getCode());
            message.setmToCode(mSuperAdmins.get(i).getmSuNo());
            message.save(new SaveListener<String>() {
                @Override
                public void done(String s, BmobException e) {

                    DialogUtils.showToast(SendMessageActivity.this, "发送成功");
                    finish();
                }
            });
        }
    }


}
